package cn.yyh.pet.service.impl;

import cn.yyh.basic.domain.Point;
import cn.yyh.basic.service.impl.BaseServiceImpl;
import cn.yyh.basic.utils.DistanceUtil;
import cn.yyh.basic.utils.PageInfo;
import cn.yyh.org.domain.Employee;
import cn.yyh.org.domain.Shop;
import cn.yyh.org.mapper.EmployeeMapper;
import cn.yyh.org.mapper.ShopMapper;
import cn.yyh.pet.domain.Pet;
import cn.yyh.pet.domain.SearchMasterMsg;
import cn.yyh.pet.mapper.PetMapper;
import cn.yyh.pet.mapper.SearchMasterMsgMapper;
import cn.yyh.pet.query.SearchMasterMsgQuery;
import cn.yyh.pet.service.IPetService;
import cn.yyh.pet.service.ISearchMasterMsgService;
import cn.yyh.user.mapper.UserMapper;
import cn.yyh.user.pojo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: SearchMasterMsgServiceImpl
 * @Package: cn.yyh.pet.service.impl
 * @date 2021/7/5
 */
@Service
public class SearchMasterMsgServiceImpl extends BaseServiceImpl<SearchMasterMsgMapper, SearchMasterMsg> implements ISearchMasterMsgService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private IPetService petService;


    @Override
    public void publish(SearchMasterMsg searchMasterMsg, Long id) {
        User user = userMapper.getByLoginInfoId(id);
        searchMasterMsg.setUser_id(user.getId());
        searchMasterMsg.setUser(user);
        //2.获取最近的店铺
        Point point = DistanceUtil.getPoint(searchMasterMsg.getAddress());
        Shop nearestShop = DistanceUtil.getNearestShop(point, shopMapper.listAll());
        searchMasterMsg.setShop(nearestShop);
        searchMasterMsg.setShop_id(nearestShop.getId());
        //3.保存寻主消息
        baseMapper.save(searchMasterMsg);

    }

    @Override
    public PageInfo<SearchMasterMsg> queryPage(SearchMasterMsgQuery query, Long id) {

        Employee emp = employeeMapper.getByLoginInfoId(id);
        if (emp.getShopId() != null) {
            query.setShopId(emp.getShopId());
        }

        return super.pageQuery(query);
    }

    @Override
    public void handlePets(Pet pet, Long id) {
        baseMapper.updateStateById(pet.getSearch_master_msg_id());

        petService.save(pet);
    }
}
